﻿//********************************************************************************//
//创建日期：<创建日期，2016-8-16>
//创建作者：<孙雪婷，guodengjie@shanghai3h.com>
//功能说明：设置计划
//********************************************************************************//
using System;
using System.Web.UI;
using Entity;
using BLL;
using System.Data;
using Web.Class;
using DevExpress.Web;

namespace Web.Pages.YongShui
{
    public partial class SheZhiJH : BasePage
    {

        #region 属性

        

        /// <summary>
        /// 当前操作人站点
        /// </summary>
        private string GuanLiKH
        {
            get
            {
                return Convert.ToString(ViewState["GuanLiKH"]);
            }
            set
            {
                ViewState["GuanLiKH"] = value;
            }
        }

        /// <summary>
        /// 当前操作人站点
        /// </summary>
        private string ZhanDianBH
        {
            get
            {
                return Convert.ToString(ViewState["ZhanDianBH"]);
            }
            set
            {
                ViewState["ZhanDianBH"] = value;
            }
        }

        /// <summary>
        /// 传过来用户号
        /// </summary>
        private string yonghuh
        {
            get
            {
                return ViewState["yonghuh"] as string;
            }
            set
            {
                ViewState["yonghuh"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 用户信息对象
        /// </summary>
        YS_YG_YONGHUXX _YS_YG_YONGHUXX = new YS_YG_YONGHUXX();

        /// <summary>
        /// 用户信息BLL
        /// </summary>  
        YS_YG_YONGHUXXBLL _YS_YG_YONGHUXXBLL = new YS_YG_YONGHUXXBLL();

        /// <summary>
        /// 计划用水
        /// </summary>  
        YS_JH_YUANJIHUA _YS_JH_YUANJIHUA = new YS_JH_YUANJIHUA();

        /// <summary>
        /// 计划用水BLL
        /// </summary>  
        YS_JH_YUANJIHUABLL _YS_JH_YUANJIHUABLL = new YS_JH_YUANJIHUABLL();

        /// <summary>
        /// 公司信息
        /// </summary>
        YS_JX_GONGSIXX _YS_JX_GONGSIXX = new YS_JX_GONGSIXX();

        /// <summary>
        /// 公司信息bll
        /// </summary>
        YS_JX_GONGSIXXBLL _YS_JX_GONGSIXXBLL = new YS_JX_GONGSIXXBLL();

        /// <summary>
        /// 局信息
        /// </summary>
        YS_JX_JUXINX _YS_JX_JUXINX = new YS_JX_JUXINX();

        /// <summary>
        /// 局信息bll
        /// </summary>
        YS_JX_JUXINXBLL _YS_JX_JUXINXBLL = new YS_JX_JUXINXBLL();

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();


        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                GetQueryString();
                LoadInfo();
            }
        }

        /// <summary>
        /// 获取URL参数
        /// </summary>
        private void GetQueryString()
        {
            if (string.IsNullOrEmpty(this.Request.QueryString["yonghuh"]))
            {
                throw new Exception("参数不正确");
            }
            this.yonghuh = this.Request.QueryString["yonghuh"];
        }

        /// <summary>
        /// 加载信息
        /// </summary>
        private void LoadInfo()
        {
            DataRowCollection drc = _YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic("S_YONGHUH='" + this.yonghuh + "' and i_leixing=1", null).Rows;
            if (drc.Count != 1)
            {
                throw new Exception("该页面只用于开户后设置计划,用户应仅有1条下月数据.请检查用户是否出错");
            }
            this.GuanLiKH = Convert.ToString(drc[0]["S_GUANLIKBH"]);
            this.ZhanDianBH = Convert.ToString(drc[0]["S_ZHANDIANBH"]);
            txtYongHuH.Text = Convert.ToString(drc[0]["S_YONGHUH"]);
            txtYongHuM.Text = Convert.ToString(drc[0]["S_YONGHUM"]);
            txtJuHao.Text = Convert.ToString(drc[0]["S_JUBIANH"]);
            txtJuHao2.Text = "";
            txtGongSiM.Text = Convert.ToString(drc[0]["S_GONGSIBH"]);
            txtGongSiM2.Text = "";

            if (txtGongSiM.Text != "")
            {
                DataTable GongSiXX = _YS_JX_GONGSIXXBLL.SelectYS_JX_GONGSIXXesDynamic(string.Format("s_bianhao='{0}' and i_leixing=1", txtGongSiM.Text), null);
                txtGongSiM2.Text = GongSiXX.Rows[0][2].ToString();
            }
            if (txtJuHao.Text != "")
            {
                DataTable JuXinX = _YS_JX_JUXINXBLL.SelectYS_JX_JUXINXesDynamic(string.Format("s_bianhao='{0}' and i_leixing=1", txtJuHao.Text), null);
                txtJuHao2.Text = JuXinX.Rows[0][3].ToString();
            }

            //如果该用户设置过计划后，将之前设置的计划显示出来
            int ym = BusinessTool.GetWaterYM();
            DataTable dtsl = _YS_JH_YUANJIHUABLL.SelectYS_JH_YUANJIHUAsDynamic(string.Format("i_jlzt=0 and i_y={0} and s_yonghuh='{1}'", ym / 100, this.yonghuh), null);
            if (dtsl != null && dtsl.Rows.Count > 0)
            {
                txtM1.Text = dtsl.Rows[0]["I_M1"].ToString(); txtM7.Text = dtsl.Rows[0]["I_M7"].ToString();
                txtM2.Text = dtsl.Rows[0]["I_M2"].ToString(); txtM8.Text = dtsl.Rows[0]["I_M8"].ToString();
                txtM3.Text = dtsl.Rows[0]["I_M3"].ToString(); txtM9.Text = dtsl.Rows[0]["I_M9"].ToString();
                txtM4.Text = dtsl.Rows[0]["I_M4"].ToString(); txtM10.Text = dtsl.Rows[0]["I_M10"].ToString();
                txtM5.Text = dtsl.Rows[0]["I_M5"].ToString(); txtM11.Text = dtsl.Rows[0]["I_M11"].ToString();
                txtM6.Text = dtsl.Rows[0]["I_M6"].ToString(); txtM12.Text = dtsl.Rows[0]["I_M12"].ToString();
            }
        }

        /// <summary>
        /// 绑定空表头
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {
            base.Render(sender);
        }


     
        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            int ym = BusinessTool.GetWaterYM();
            int m = ym % 100;
            for (int i = 1; i <= 12; i++)
            {
                ((ASPxTextBox)this.FindControl("txtM" + i)).Text = "0";
            }
            this.txtMSum.Text = "0";
            for (int i = 1; i <= m; i++)
            {
                ((ASPxTextBox)this.FindControl("txtM" + i)).ClientEnabled = false;
            }
            ((ASPxTextBox)this.FindControl("txtM" + (m + 1))).Focus();
        }



        /// <summary>
        /// 保存数据
        /// </summary>
        private bool SaveData()
        {
            YS_JH_YUANJIHUA _YS = new YS_JH_YUANJIHUA();
            DataRowCollection drc = _YS_JH_YUANJIHUABLL.SelectYS_JH_YUANJIHUAsDynamic("S_YONGHUH='"+txtYongHuH.Text+"'",null).Rows;
            if(drc.Count>1)
            {
                throw new Exception("查询到了多条原计划,请检查数据正确性");
            }
            if (drc.Count == 1)
            {
                _YS.ID = Convert.ToDecimal(drc[0]["ID"]);
            }
            else
            {
                _YS.ID = 0;
            }
            _YS.S_YONGHUH = txtYongHuH.Text;
            _YS.I_TYPE = 4;
            _YS.I_Y = BusinessTool.GetWaterYM()/100;
            _YS.S_GUANLIKBH = this.GuanLiKH;
            _YS.S_ZHANDIANBH = this.ZhanDianBH;
            _YS.S_JUBIANH = this.txtJuHao.Text;
            _YS.S_GONGSIBH = this.txtGongSiM.Text;
            _YS.I_M1 = Convert.ToInt32(txtM1.Text); _YS.I_M2 = Convert.ToInt32(txtM2.Text);
            _YS.I_M3 = Convert.ToInt32(txtM3.Text); _YS.I_M4 = Convert.ToInt32(txtM4.Text);
            _YS.I_M5 = Convert.ToInt32(txtM5.Text); _YS.I_M6 = Convert.ToInt32(txtM6.Text);
            _YS.I_M7 = Convert.ToInt32(txtM7.Text); _YS.I_M8 = Convert.ToInt32(txtM8.Text);
            _YS.I_M9 = Convert.ToInt32(txtM9.Text); _YS.I_M10 = Convert.ToInt32(txtM10.Text);
            _YS.I_M11 = Convert.ToInt32(txtM11.Text); _YS.I_M12 = Convert.ToInt32(txtM12.Text);
            _YS.I_JLZT = 0;
            _YS.S_CAOZUOR = LoginInfo.CurrentUser.Account;
            _YS.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            _YS.D_APPLY = BusinessTool.GetDataBaseDateTime();
            _YS.D_KNOWN = BusinessTool.GetDataBaseDateTime();
            _SYS_LOGBLL.Log<YS_JH_YUANJIHUA>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "开户-设置计划", OperateState.Add, _YS, null);
            if (_YS_JH_YUANJIHUABLL.InsertOrUpdate(_YS))
            {
                
                return true;
            }
            return false;
        }


        /// <summary>
        /// 前台回调
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            CallbackResult cr = new CallbackResult(e.Parameter);
            if (e.Parameter == "save")
            {
                if ( SaveData())
                {
                    cr.IsSuccess = true;
                    cr.IsAlertMsg = true;
                    cr.Msg = "保存成功！";
                    e.Result = cr.ToString();
                }

            }
        }
        #endregion
    }
}